/*
 * BanBu_LocationCell
 * @description 本文件提供聊天界面对话界面显示地理位置消息的cell
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供聊天界面对话界面显示地理位置消息的cell
 */
#import "BanBu_LocationCell.h"
#import "BanBuAPIs.h"

@implementation BanBu_LocationCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tempBtn.frame = CGRectZero;
        [tempBtn addTarget:self action:@selector(MakeMap:) forControlEvents:UIControlEventTouchUpInside];
        
        _mapButton=tempBtn;
        tempBtn.tag=10101;
        
        [self addSubview:tempBtn];
    }
    return self;
}

-(void)dealloc
{
    [_mapView release],_mapView = nil;
    [super dealloc];
}

-(void)layoutSubviews
{
    self.bkViewWidth = 160.0;
    
    [super layoutSubviews];
    
    self.bkView.alpha = 1;
    
    CGRect frame = locationViewRext00;
    
    _mapView.frame = frame;
    
//    if (self.bubbleID == 0)
    {
        CGRect frame = _mapView.frame;
        frame.origin.y += 4;
        if (!self.atLeft)
        {
            frame.origin.x += 3;
        }
        _mapView.frame = frame;
        _mapView.layer.cornerRadius = 12;
    }
    
    self.avatar.frame = AvatarRect01;
//    [self.vipImageView setFrame:CGRectMake(self.avatar.frame.origin.x + 28, self.avatar.frame.origin.y + 26, 15, 10)];
}

//设置cell的各种信息
-(void)setChatCellInfo:(NSDictionary *)amsg
{
    //设置父类的属性
    [super setChatCellInfo:amsg];
    
    NSArray *firstWords = [[amsg valueForKey:KeyContent] componentsSeparatedByString:@","];
    if(firstWords.count>1)
    {
        CLLocationDegrees lat = [[firstWords objectAtIndex:1]floatValue]/1000000;
        CLLocationDegrees lon = [[firstWords objectAtIndex:0] floatValue]/1000000;
        
        [self setLocationLat:lat andLong:lon];
    }
}


- (void)setLocationLat:(CLLocationDegrees)lat andLong:(CLLocationDegrees)lon
{
    [self performSelectorOnMainThread:@selector(setCustMapView) withObject:nil waitUntilDone:YES];
    _mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(lat, lon), MKCoordinateSpanMake(0.005, 0.005));
    
    [self performSelectorOnMainThread:@selector(setMapBtnFrame) withObject:nil waitUntilDone:YES];
    
    [self bringSubviewToFront:_mapButton];
}

-(void)setMapBtnFrame
{
    _mapButton.frame =CGRectMake(79, 12, 163, 64);
}

-(void)setCustMapView
{
    if(!self.mapView)
    {
        _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(89, 12, 160, 12)];
        
        _mapView.frame = CGRectMake(89, 12, 160, 64);
        _mapView.userInteractionEnabled = YES;
    }
    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;
    
    [self addSubview:self.mapView];
    
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;
    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
        pinView = [[[MKPinAnnotationView alloc]
                    initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.canShowCallout = YES;
    pinView.animatesDrop = NO;
    return pinView;
}

-(void)MakeMap:(UIButton *)tap
{
    if([self.delegate respondsToSelector:@selector(MakeBigMap)])
    {
        [self.delegate MakeBigMap];
    }
}
@end
